package com.eps.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

/**
 * 
 * @author Renato
 */
public class Utiles_Fecha {
	private String dia;
	private String mes;
	private String anio;
	private String nom_dia;
	private String nom_mes;

	public Utiles_Fecha(String d, String m, String a) {
		this.dia = d;

		this.mes = m;
		this.anio = a;
	}

	public Utiles_Fecha(Date f) {
		StringTokenizer str = new StringTokenizer(f.toString());
		this.nom_dia = str.nextToken();
		this.mes = identificaMes(str.nextToken());
		this.dia = str.nextToken();
		str.nextToken();
		str.nextToken();
		this.anio = str.nextToken();
	}

	public Utiles_Fecha(Date f, String d) {
		StringTokenizer str = new StringTokenizer(f.toString(), d);
		str.nextToken();
		this.mes = identificaMes(str.nextToken());
		this.dia = str.nextToken();
		str.nextToken();
		str.nextToken();
		this.anio = str.nextToken();
	}

	public Utiles_Fecha(String fec) {
		StringTokenizer str = new StringTokenizer(fec, "/");
		this.dia = str.nextToken();
		this.mes = str.nextToken();
		this.anio = str.nextToken();
	}

	public String getDia() {
		return this.dia;
	}

	public String getMes() {
		return this.mes;
	}

	public String getAnio() {
		return this.anio;
	}

	public String getFecha() {
		return this.dia + "/" + this.mes + "/" + this.anio;
	}

	public Date getDate() {
		int a = Integer.parseInt(this.anio);
		int m = Integer.parseInt(this.mes);
		int d = Integer.parseInt(this.dia);
		return new Date(a - 1900, m - 1, d);
	}

	public boolean isValida() {
		int a = Integer.parseInt(this.anio);
		int m = Integer.parseInt(this.mes);
		int d = Integer.parseInt(this.dia);

		if ((d > 31) || (d <= 0)) {
			return false;
		}

		return (m <= 12) && (m > 0);
	}

	public void setDia(String d) {
		this.dia = d;
	}

	public void setMes(String m) {
		this.mes = m;
	}

	public void setAnio(String a) {
		this.anio = a;
	}

	public static String[] proyectaMesFechas(String fec, int p) {
		String[] proy = new String[p];
		String fecTemp = fec;
		for (int i = 0; i < p; ++i) {
			fecTemp = siguienteMes(fecTemp).getFecha();
			proy[i] = fecTemp;
		}
		return proy;
	}

	public static Utiles_Fecha[] proyectaMesFechas2(String fec, int p) {
		Utiles_Fecha[] proy = new Utiles_Fecha[p];
		String fecTemp = fec;
		Utiles_Fecha temp = null;
		for (int i = 0; i < p; ++i) {
			temp = siguienteMes(fecTemp);
			proy[i] = temp;
			fecTemp = temp.getFecha();
		}
		return proy;
	}

	public String getNombreMes(String mes) {
		if (mes.equals("01"))
			return "Enero";
		if (mes.equals("02"))
			return "Febrero";
		if (mes.equals("03"))
			return "Marzo";
		if (mes.equals("04"))
			return "Abril";
		if (mes.equals("05"))
			return "Mayo";
		if (mes.equals("06"))
			return "Junio";
		if (mes.equals("07"))
			return "Julio";
		if (mes.equals("08"))
			return "Agosto";
		if (mes.equals("09"))
			return "Septiembre";
		if (mes.equals("10"))
			return "Octubre";
		if (mes.equals("11"))
			return "Noviembre";
		if (mes.equals("12")) {
			return "Diciembre";
		}
		return null;
	}

	public static String identificaMes(String mes) {
		if (mes.equals("Jan"))
			return "01";
		if (mes.equals("Feb"))
			return "02";
		if (mes.equals("Mar"))
			return "03";
		if (mes.equals("Apr"))
			return "04";
		if (mes.equals("May"))
			return "05";
		if (mes.equals("Jun"))
			return "06";
		if (mes.equals("Jul"))
			return "07";
		if (mes.equals("Aug"))
			return "08";
		if (mes.equals("Sep"))
			return "09";
		if (mes.equals("Oct"))
			return "10";
		if (mes.equals("Nov"))
			return "11";
		if (mes.equals("Dec")) {
			return "12";
		}
		return null;
	}

	public static Utiles_Fecha siguienteMes(String fec) {
		Utiles_Fecha utemp = new Utiles_Fecha(fec);

		int val = getSigMes(Integer.parseInt(utemp.getMes()));
		if (val != -1) {
			String m = null;
			if (val != 0) {
				m = "" + val;
			} else {
				utemp.setAnio(""
						+ getSigAnio(Integer.parseInt(utemp.getAnio())));
				m = "1";
			}
			utemp.setMes(m);
		}
		return utemp;
	}

	static int getSigMes(int m) {
		if ((m > 0) && (m < 12))
			return m + 1;
		if (m == 12) {
			return 0;
		}
		return -1;
	}

	public String getNomDia() {
		return identificaDia();
	}

	public String identificaDia() {
		if (this.nom_dia.equalsIgnoreCase("Mon"))
			this.nom_dia = "Lunes";
		else if (this.nom_dia.equalsIgnoreCase("Tue"))
			this.nom_dia = "Martes";
		else if (this.nom_dia.equalsIgnoreCase("Wed"))
			this.nom_dia = "Miercoles";
		else if (this.nom_dia.equalsIgnoreCase("Thu"))
			this.nom_dia = "Jueves";
		else if (this.nom_dia.equalsIgnoreCase("Fri"))
			this.nom_dia = "Viernes";
		else if (this.nom_dia.equalsIgnoreCase("Sat"))
			this.nom_dia = "Sabado";
		else if (this.nom_dia.equalsIgnoreCase("Sun")) {
			this.nom_dia = "Domingo";
		}
		return this.nom_dia;
	}

	 public static Date obtieneFechaSuma(Date fecha,int dias){
         Calendar cal = new GregorianCalendar();
        try {
            
             cal.setTimeInMillis(fecha.getTime());
             cal.add(Calendar. DATE, dias);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

         return new Date(cal.getTimeInMillis());

     //   return ""+cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR);

    }
	 
	 public static Date sumarDias(Date date,int dias){
	        Calendar cal = new GregorianCalendar();
	         cal.setTimeInMillis(date.getTime());
	         cal.add(Calendar. DATE, dias);
	         return new Date(cal.getTimeInMillis());
	    }
	 
	static int getSigAnio(int a) {
		return a + 1;
	}
}